//app.js
var qcloud = require('./vendor/wafer2-client-sdk/index')
var config = require('./config')


App({

	globalData: {
		openid: null, //自定义  
    userInfo: null,
    logged: false,
	},  


  onLaunch: function () {
		var page=this;
		wx.login({
			success: function (res) {
				if (res.code) {
					//获取openId
					wx.request({
						url: 'https://api.weixin.qq.com/sns/jscode2session',
						data: {
							　　　　　　　
							appid: 'wxfb8238e32d0330a4',
							//小程序的 app secret
							secret: '3118e56a225d0e994c65b3e1d1dbb239',
							grant_type: 'authorization_code',
							js_code: res.code
						},
						method: 'GET',
						header: { 'content-type': 'application/json' },
						success: function (openIdRes) {
							console.info("登录成功返回的openId：" + openIdRes.data.openid);
							page.globalData.openid= openIdRes.data.openid;
							console.log(page.globalData.openid);
							// 判断openId是否获取成功
							if (openIdRes.data.openid != null & openIdRes.data.openid != undefined) {
								　　　　　　　　// 有一点需要注意 询问用户 是否授权 那提示 是这API发出的
								wx.getUserInfo({
									success: function (data) {
										// 自定义操作
										// 绑定数据，渲染页面
                    // page.globalData.userInfo=data.userInfo,
                    // page.globalData.logged=true,
                    //   console.log(data.userInfo)
									},
									fail: function (failData) {
										console.info("用户拒绝授权");
									}
								});
							} else {
								console.info("获取用户openId失败");
							}
						},
						fail: function (error) {
							console.info("获取用户openId失败");
							console.info(error);
						}
					})
				}
			}
		});
    var books = wx.getStorageSync('books');
    if (!books) {
      books = this.loadBooks();
    }
    wx.setStorageSync('books', books);
    var sbooks = wx.getStorageSync('sbooks');
    if (!sbooks) {
      sbooks = this.loadsBooks();
    }
    wx.setStorageSync('sbooks', sbooks);


  },


  loadBooks: function () {
    var books=new Array();
    var book=new Object();
    book.id="1";
    book.pic='../images/book/1.jpeg';
    book.name="只有时间知道";
    book.writer="啦啦啦";
    book.price="22.00";
    book.fprice="25.00";
    book.date="2018-5-23";
    book.sellername="yao";
    books[0]=book;

    return books;
  },
   loadsBooks: function () {
    var sbooks = new Array();
    var sbook = new Object();
    sbook.id = "1";
    sbook.name = "只有时间知道";
    sbook.writer = "啦啦啦";
    sbook.buyername = "yao";
    sbooks[0] = sbook;

    return sbooks;
  },
})